package com.imprenta.ejb;

import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.hibernate.util.HibernateUtil;
import com.imprenta.entities.Cliente;
import com.imprenta.entities.Login;

public class EjbRegistrarCliente {
	
	public static boolean SaveCliente(Cliente cliente){
		
		boolean success = false;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Transaction transaction = session.beginTransaction();
			session.save(cliente);
			transaction.commit();
			success = true;
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return success;
	}//end savecliente
	
	public static Cliente getClienteById(int _id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Cliente c = new Cliente();
		try {
			String q = "select c.nombre, c.apellido, c.direccion, c.telefono, c.movil " + 
					"from Cliente c where c.clienteid = :id";
			Query query = session.createQuery(q);
			query.setParameter("id", _id);
			
			for (int i = 0; i < query.list().size(); i++) {
				Object[] element = (Object[])query.list().get(i);
				c.setNombre(element[0].toString());
				c.setApellido(element[1].toString());
				c.setDireccion(element[2].toString());
				c.setTelefono(element[3].toString());
				c.setMovil(element[4].toString());
			}
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}
		}
		return c;
	}//end getclientebyid
	
	public static boolean updateClienteById(Cliente c){
		Session session = HibernateUtil.getSessionFactory().openSession();
		boolean success = false;
		try {
			Transaction transaction = session.beginTransaction();
			Cliente cliente = (Cliente) session.get(Cliente.class, c.getClienteid());
			cliente.setClienteid(c.getClienteid());
			cliente.setNombre(c.getNombre());
			cliente.setApellido(c.getApellido());
			cliente.setDireccion(c.getDireccion());
			cliente.setTelefono(c.getTelefono());
			cliente.setMovil(c.getMovil());
			session.saveOrUpdate(cliente);
			transaction.commit();
			success = true;
		} catch (HibernateException he) {
			// TODO: handle exception
			success = false;
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			success = false;
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}
		}
		return success;
	}//end updateclientebyid
	
	public static boolean updateClienteById(Cliente c, Login l){
		Session session = HibernateUtil.getSessionFactory().openSession();
		boolean success = false;
		try {
			Transaction transaction = session.beginTransaction();
			Cliente cliente = (Cliente) session.get(Cliente.class, c.getClienteid());
			//Cliente cliente = new Cliente();
			cliente.setClienteid(c.getClienteid());
			cliente.setNombre(c.getNombre());
			cliente.setApellido(c.getApellido());
			cliente.setDireccion(c.getDireccion());
			cliente.setTelefono(c.getTelefono());
			cliente.setMovil(c.getMovil());
			session.saveOrUpdate(cliente);
			
			
			session.update(l);
			
			transaction.commit();
			success = true;
		} catch (HibernateException he) {
			// TODO: handle exception
			success = false;
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			success = false;
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}
		}
		return success;
	}//end updateclientebyid
	
	public static boolean SaveLogin(String user, String pass, String rol){
		
		boolean success = false;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Login login = new Login();
			login.setUsername(user);login.setPassword(pass); login.setRol(rol);
			Transaction transaction = session.beginTransaction();
			session.save(login);
			transaction.commit();
			success = true;
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return success;
	}//end save login
	@SuppressWarnings("rawtypes")
	public static Login GetLogin(int _id){
		
		Login l = new Login();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String query = 	"select l.idlogin, l.username,l.password,l.rol from Login l inner join l.clientes c where c.clienteid = :id";
			Query q = session.createQuery(query);
			q.setParameter("id",_id);
			q.setMaxResults(1);
			List lista = q.list();
			
			for (int i = 0; i < lista.size(); i++) {
				Object[] items = (Object[])lista.get(i);
				
				l.setIdlogin(Integer.parseInt(items[0].toString()));
				l.setUsername(items[1].toString());
				l.setPassword(items[2].toString());
				l.setRol(items[3].toString());
			}
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return l;
	}//end getlogin
	

	@SuppressWarnings("rawtypes")
	public static Login GetLoginById(){
		
		Login l = new Login();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String query = "select idlogin, username, password, rol from Login order by idlogin desc";
			Query q = session.createQuery(query);
			q.setMaxResults(1);
			List lista = q.list();
			
			for (int i = 0; i < lista.size(); i++) {
				Object[] items = (Object[])lista.get(i);
				
				l.setIdlogin(Integer.parseInt(items[0].toString()));
				l.setUsername(items[1].toString());
				l.setPassword(items[2].toString());
				l.setRol(items[3].toString());
			}
//			id = Integer.parseInt(q.list().get(0).toString());
//			List l = q.list();
//			Object[] item = (Object[]) l.get(0);
//			id = Integer.parseInt(item[0].toString());
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return l;
	}//end getloginbyid
	
	@SuppressWarnings("rawtypes")
	public static boolean VerifUsername(String userName){
		
		boolean isAvailable = false;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String q = "from Login l where l.username like :username";
			Query query = session.createQuery(q);
			query.setParameter("username", userName);
			
			List l = query.list();
			if (l.size() >= 1) {
				isAvailable = false;
			}else if (l.size() <= 0) {
				isAvailable = true;
			}else {
				isAvailable = true;
			}
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return isAvailable;
	}//fin verifusername
}
